﻿#region 程序集引用
using System;
using System.Security.Cryptography;
#endregion

#region 版权注释
/*===========================================================================
// Copyright (C) 2017 Zhang.cn，并保留所有 
// 作者：吴岸标 QQ:405403610
// 创建日期：2017-09-03
// 功能描述：推荐码生成
===========================================================================*/
#endregion

namespace Zhang
{
    namespace Utils
    {
        /// <summary>
        /// 推荐码生成
        /// </summary>
        public class RecCode
        {
            private static string[] Letter0 = new string[] { "x", "y", "k", "p", "9", "4", "h", "m", "n", "5" };
            private static string[] Letter1 = new string[] { "a", "i", "q", "r", "2", "3", "t", "s", "6", "e" };
            private static string[] Letter2 = new string[] { "b", "c", "f", "g", "j", "0", "u", "v", "w", "z" };
            private static string[] Letter = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

            /// <summary>
            /// 生成推荐码
            /// </summary>
            /// <returns></returns>
            public static string Create()
            {
                string code;
                int second = (DateTime.Now.Year - 2014) * 366 * 24 * 3600 + DateTime.Now.Hour * 3600 + DateTime.Now.Minute * 60 + DateTime.Now.Second + DateTime.Now.Millisecond;
                string seed = second.ToString();
                string[] currLetter;
                code = "";

                switch (new Random(second).Next(0, 3))
                {
                    case 0:
                        currLetter = Letter0;
                        break;
                    case 1:
                        currLetter = Letter1;
                        break;
                    case 2:
                        currLetter = Letter2;
                        break;
                    default:
                        currLetter = Letter0;
                        break;
                }

                for (int i = 0; i < seed.Length; i++)
                {
                    code += currLetter[int.Parse(seed.Substring(i, 1))];
                }

                code = Letter[new Random(second).Next(0, 24)] + code;
                return code;

            }

        }
    }
}
